package com.cx.mq.provider;

import com.cx.common.constant.MqConstant;
import com.cx.common.data.MqMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author:chenxiaoyi
 * @Date:2020/8/8 13:54
 */
@Controller
public class RabbitMqProvider {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * P1.测试消息发送到direct交换机
     *      发送java对象 -- 实现序列化
     *      发送String
     */
    @GetMapping("/sendMsgP1")
    @ResponseBody
    public String sendMsgP1(){
        rabbitTemplate.convertAndSend(MqConstant.Exchange.DIRECT,MqConstant.RoutingKey.D_ROUTINGKEY_O,"work模式");
        MqMessage msg = MqMessage.builder().id(1L).time(new Date()).build();
        rabbitTemplate.convertAndSend(MqConstant.Exchange.DIRECT,MqConstant.RoutingKey.D_ROUTINGKEY_O,msg);
        return "OK";
    }


    /**
     * P2.测试消息发送到direct交换机
     *    work -- 多个消费者模式
     *    ack -- 能者多劳模式
     */
    @GetMapping("/sendMsgP2")
    @ResponseBody
    public String sendMsgP2(){
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(MqConstant.Exchange.DIRECT,MqConstant.RoutingKey.D_ROUTINGKEY,"work模式"+i);
        }
        return "OK";
    }


    /**
     * P3.测试消息重发次数并记录redis
     *      加入布隆过滤放重复处理
     */



    /**
     * P4.测试消息发送到fanout交换机
     */

    @GetMapping("/sendMsgP4")
    @ResponseBody
    public String sendMsgP4(){
        rabbitTemplate.convertAndSend(MqConstant.Exchange.FANOUT,"","--广播消息--");
        return "OK";
    }



    /**
     * 3.测试消息发送到topic交换机
     *      测试匹配不同路由
     */

    @GetMapping("/sendMsgP5")
    @ResponseBody
    public String sendMsgP5(){
        rabbitTemplate.convertAndSend(MqConstant.Exchange.FANOUT,"","--广播消息--");
        return "OK";
    }






    /**
     * 5.死信队列
     */
    
    

    /**
     * 6.消息100%投递两种方案
     */





}
